package com.dykj.fireFighting.web.firemanger;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.firemanger.entity.SafetySystem;
import com.dykj.fireFighting.api.socialUnit.firemanger.dto.SafetySystemDto;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.ISafetySystemService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 消防安全-安全制度接口
 *
 * @Author aixiaolan
 * @Date  2021-06-24 10:13:42
 */
@Slf4j
@Api(value = "消防安全-安全制度接口", tags = {"消防安全-安全制度接口"})
@RestController
@RequestMapping("firemanger/safetySystem")
public class SafetySystemController {

    @DubboReference
    private ISafetySystemService safetySystemService;

    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }


    /**
     * 消防安全-安全制度列表
     *
     * @return
     */
    @AutoLog(value = "消防安全-安全制度-分页列表查询")
    @ApiOperation(value = "消防安全-安全制度分页列表", notes = "消防安全-安全制度分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<SafetySystem>> treeGrid(SafetySystemDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<SafetySystem> page = safetySystemService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消防安全-安全制度信息
     */
    @AutoLog(value = "消防安全-安全制度-获取消防安全-安全制度信息")
    @ApiOperation(value = "获取消防安全-安全制度信息", notes = "获取消防安全-安全制度信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<SafetySystem> info(@PathVariable("id") String id){
        SafetySystem safetySystem = safetySystemService.getById(id);
        return CommonResult.success(safetySystem);
    }

    /**
     * 保存消防安全-安全制度信息
     */
    @AutoLog(value = "消防安全-安全制度-保存消防安全-安全制度信息")
    @ApiOperation(value = "保存消防安全-安全制度信息", notes = "保存消防安全-安全制度信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody SafetySystem safetySystem) {
        try {
            safetySystem.setCreateTime(new Date());
            safetySystem.setUpdateTime(new Date());
            safetySystem.setCreateBy(getName());
            safetySystem.setUnitId(getEnterpriseId());
                safetySystemService.save(safetySystem);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改消防安全-安全制度信息
     */
    @AutoLog(value = "消防安全-安全制度-修改消防安全-安全制度信息")
    @ApiOperation(value = "修改消防安全-安全制度信息", notes = "修改消防安全-安全制度信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody SafetySystem safetySystem) {
        try {
            safetySystem.setUpdateTime(new Date());
            safetySystem.setUpdateBy(getName());
                safetySystemService.updateById(safetySystem);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除消防安全-安全制度信息
     */
    @AutoLog(value = "消防安全-安全制度-删除消防安全-安全制度信息")
    @ApiOperation(value = "删除消防安全-安全制度信息", notes = "删除消防安全-安全制度信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                safetySystemService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
